// pages/qingjia/qingjia.js
const app = getApp()
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shijiavalue: '',
    startshijiashow: false,
    endshijiashow: false,
    minHour: 0,
    maxHour: 23,
    minDate: new Date().getTime(),
    maxDate: new Date(2100, 12, 31).getTime(),
    currentshijiaStartDate: new Date().getTime(),
    currentshijiaEndDate: new Date().getTime(),
    startTimeshijia:null,
    endTimeshijia:null,
    type:1,
    content:[{"text":"事假","value":1},{"text":"病假","value":2} , {"text":"工伤假","value":3},{"text":"婚假","value":4},
    {"text":"丧家","value":5}, 
    {"text":"产假","value":6},
    {"text":"年休假","value":7},
    {"text":"公假","value":8},
    {"text":"探亲假","value":9}]
  },
  onclasschange({
    detail
  }) {
    this.setData({
      type: detail
    });

  },
  onshijiaStartTimeInput(event) {
    console.log(event.detail)
    this.setData({
      currentshijiaStartDate: event.detail,
    });
  },
  
  onshijiaStartConfirm(event) {
    var timeValue = this.timeFormat(new Date(this.data.currentshijiaStartDate), "yyyy-MM-dd hh:mm");
    this.setData({ startTimeshijia: timeValue, startshijiashow: false });
  },
  timeFormat(date, fmt) {
    var o = {
      "M+": date.getMonth() + 1,                 //月份   
      "d+": date.getDate(),                    //日
      "h+": date.getHours(),                   //小时   
      "m+": date.getMinutes(),                 //分   
      "s+": date.getSeconds(),                 //秒   
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
      "S": date.getMilliseconds()             //毫秒   
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
   
  },

  onshihjiaStartCancel() {
    this.setData({ startshijiashow: false });
  },

  showShijJiaPopup() {
    this.setData({ startshijiashow: true });
  },

  onshijiaStartClose() {
    this.setData({ startshijiashow: false });
  },

  
  endShiJiaPopup(){
    this.setData({ endshijiashow: true });
  },
  onshijiaEndClose() {
    this.setData({ endshijiashow: false });
  },
  onshihjiaEndCancel() {
    this.setData({ endshijiashow: false });
  },
  onshijiaEndTimeInput(event) {
    console.log(event.detail)
    this.setData({
      currentshijiaEndDate: event.detail,
    });
  },

  onshijiaEndConfirm(event) {
    var timeValue = this.timeFormat(new Date(this.data.currentshijiaEndDate), "yyyy-MM-dd hh:mm");
    this.setData({ endTimeshijia: timeValue, endshijiashow: false });
  },

  onShijiaInputChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
    this.setData({
      shijiavalue:event.detail
    })
 
  },

 

  onChange(event) {
    // wx.showToast({
    //   title: `切换到标签 ${event.detail.name}`,
    //   icon: 'none',
    // });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  sendData:function(date,beginTime,endTime,leaveReason){
    var that=this;
    var data={"userid":app.globalData.userId,"schoolid": app.globalData.schoolId,
    "date":date,"beginTime":beginTime,"endTime": endTime,
    "leaveType":this.data.type,
    "leaveReason":leaveReason,
    "appType":app.globalData.appType,"appKey":app.globalData.appKey,"timeSpan":app.globalData.timespan,"mobileSerialnumber":""}
    wx.request({
      url: app.globalData.serverUrl,
      data: {       
        "data":JSON.stringify(data),
        'act':"send_leave"
      },
      method: 'POST',
      header: {
        'content-type':'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if(res.data.status==0){
          wx.showToast({
            title: "请假成功",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          wx.navigateBack({
            url: '../teacherattendance/teacherattendance',
          })
        }else{
          var msg = res.data.msg;
         if(msg=="LEAVE_EXISTS"){
          wx.showToast({
            title: "请假时间冲突，请勿重复提交",
            icon: 'none',
            duration: 1000,
            mask: true
          })
         }
        else{
          wx.showToast({
            title: "请假失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
         
        }
       } ,
      fail: function (res) {
        wx.showToast({
          title: "请假失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },
  shijiaBtn:function(){

    if(this.data.startTimeshijia==null || this.data.startTimeshijia.length<=0){
      wx.showToast({
        title: "请选择起始日期",
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return;
    }
    
    if(this.data.endTimeshijia==null || this.data.endTimeshijia.length<=0){
      wx.showToast({
        title: "请选择结束日期",
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return;
    }

    if(this.data.shijiavalue==null || this.data.shijiavalue.length<=0){
      wx.showToast({
        title: "请输入请假事由",
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return;
    }



   this.sendData(utils.formatDateTime1(new Date()),this.data.startTimeshijia+":00",this.data.endTimeshijia+":00",this.data.shijiavalue);
  },
 


  seeLeave:function(){
    wx.navigateTo({
      url: '../qingjiajilu/qingjiajilu',
    })
  }



})